<?php
/**                                          
 * 收藏
 * @author M2
 * @version 1.0
 * create Date 2013-04-27
 */
class FavoriteModel extends Model {
    var $tableName = 'weibo_favorite';
    static $user_cache = array();
    static $cacheHash = array();
	
    /**
     * 收藏微博	
     * @param int $id 微博ID
     * @param int $uid 用户ID
     */
    function favWeibo( $id ,$uid ){
    	$data['uid']    = $uid;
    	$data['weibo_id']  = $id;
    	$res = $this->add($data);
    	if($res){
    	    $this->_addFavoriteCache($uid, $id);
    	}
    	return $res;
    }
	
    /**
     * 取消收藏	
     * @param int $id 微博ID
     * @param int $uid 用户ID
     */
    function dodelete($id,$uid){
    	$res = $this->where("weibo_id=$id AND uid=$uid")->delete();
		if($res){
    	    $this->_delFavoriteCache($uid, $id);
    	}
		return $res;
	}
    /**
     * 检查给定用户是否收藏给定微博
     * @param int 		 $weibo_id 		 微博ID
     * @param int 		 $uid      		 用户ID
     * @param array|null $weibo_id_array $weibo_id所属的微博集合(不为空时会一次性查询, 以减少数据库请求数)
     * @param string     $key            为防止前一次调用对后一次调用的干扰, 为每个$weibo_id_array赋予唯一key
     * @return int 已收藏返回1, 否则返回0
     */
    function isFavorited($weibo_id, $uid,$static = false){
        if($static){
            self::$user_cache = $this->_getFavoriteCache($uid);
            if(empty(self::$user_cache)){
                $list = $this->_setFavoriteCache($uid);
            }else{
                $list = self::$user_cache;
            }
        }else{
            $list = $this->_getFavoriteCache($uid);
        }
        return in_array($weibo_id,$list);
    }
    
    /**
     * 获取用户微博	
     * @param int $uid 用户ID
     */
    private function _getListData($uid){
    	if(isset(self::$cacheHash[$uid])){
    		return self::$cacheHash[$uid]['data'];
    	}
       	self::$cacheHash[$uid]['data'] = $this->field('weibo_id')->where("uid=$uid")->select();
        return self::$cacheHash[$uid]['data'];
    }
    
    /**
     * 获取收藏的缓存数据	
     * @param int $uid 用户ID
     */
    private function _getFavoriteCache($uid){
        return json_decode(S('favorite_'.$uid));
    }
    
    /**
     * 添加收藏缓存	
     * @param int $uid 用户ID
     * @param int $weibo_id 微博ID
     */
    private function _addFavoriteCache($uid,$weibo_id){
        $cache = $this->_getFavoriteCache($uid);
        if(false == $cache){
            return $this->_setFavoriteCache($uid);
        }else{
            !in_array($weibo_id,$cache) && $cache[] = $weibo_id;
        }
        return S('favorite_'.$uid,json_encode($cache));
    }
    
    /**
     * 设置收藏缓存	
     * @param int $uid 用户ID
     */
    private function _setFavoriteCache($uid){
        $list = common('Array')->getSubByKey($this->_getListData($uid),'weibo_id');
        S('favorite_'.$uid,json_encode($list));
        return $list;
    }
    
    /**
     * 删除收藏缓存数据	
     * @param int $uid 用户ID
     * @param int $weibo_id 微博ID
     */
	private function _delFavoriteCache($uid,$weibo_id){
       S('favorite_'.$uid,Null);
	   return true;
    }
}
?>